package com.kingyun.gpsinspection.purificationservice.services.som.outerservice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.RedisValue;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.FacadeDataItem;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.FacadeWorkOrder;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.UnitStepResult;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;



/**
 * Created by yangcs on 2017/3/16.
 */
public class OutWorkOrderServiceImpl implements OutWorkOrderService {

    private static Logger logger = LoggerFactory.getLogger(OutWorkOrderServiceImpl.class);
    @Autowired
    private FacadeWorkOrderService facadeWorkOrderService;
    @Autowired
    private SomOperatingUnitFormItemService operatingUnitFormItemService;
    @Autowired
    private SomWorkOrderService workOrderService;
    @Autowired
    private SomFileService fileService;

    /**
     * 查询工单信息
     *
     * @param workOrderId
     * @return
     */
    @Override
    public FacadeWorkOrder getOrderById(String workOrderId) {
        logger.info("查询工单信息接口 查询参数：工单Id,workOrderId:{}", workOrderId);
        FacadeWorkOrder order = facadeWorkOrderService.getOrder(workOrderId);
        logger.info("查询工单信息接口 返回数据：" + JSON.toJSONString(order));
        return order;
    }

    /**
     * 根据cellId查询操作单元表单（含结果）
     *
     * @param cellId
     * @param cellResultId
     * @return
     */
    @Override
    public com.kingyun.gpsinspection.purificationservice.facade.model.som.OperatingUnitForm getUnitFormByCellId(String cellId, String cellResultId, BigDecimal version) {
        logger.info("查询操作单元表单接口 查询参数：操作单元Id,cellId:{}", "工单结果id,cellResultId:{}", "版本信息,version:{}", cellId, cellResultId, version);
        com.kingyun.gpsinspection.purificationservice.facade.model.som.OperatingUnitForm ouf = operatingUnitFormItemService.getUnitFormByCellId(cellId, cellResultId, version);
        logger.info("查询操作单元表单接口 返回数据：" + JSON.toJSONString(ouf));
        return ouf;
    }

    /**
     * 根据工单id，操作单元/工作内容id，版本 查询步骤信息
     *
     * @param workOrderId  工单id
     * @param cellId       操作单元/工作内容id
     * @param stepVersion  版本
     * @param cellResultId 工单对应完成id
     * @return
     */
    @Override
    public List<UnitStepResult> findByWorkOrderId(String workOrderId, String cellId, BigDecimal stepVersion, String cellResultId) {
        logger.info("查询步骤信息接口 查询参数：工单Id,workOrderId:{}", "操作单元id,cellId:{}", "版本信息,stepVersion:{}", workOrderId, cellId, stepVersion);
        List<UnitStepResult> list = workOrderService.findByWorkOrderId(workOrderId, cellId, stepVersion, cellResultId);
        logger.info("查询步骤信息接口 返回数据：" + JSON.toJSONString(list));
        return list;
    }

    /**
     * 根据步骤Id查询数据项
     *
     * @param stepId
     * @param stepResultId
     * @return
     */
    @Override
    public List<FacadeDataItem> getDataItemsByStepId(String stepId, String stepResultId) {
        logger.info("根据步骤Id查询数据项接口 查询参数：步骤Id,stepId:{}", "步骤结果id,stepResultId:{}", stepId, stepResultId);
        List<FacadeDataItem> list = facadeWorkOrderService.getDataItemsByStepId(stepId, stepResultId);
        logger.info("根据步骤Id查询数据项接口 返回数据：" + JSON.toJSONString(list));
        return list;
    }

    /**
     * 根据业务id和类型查询文件
     *
     * @param businessId
     * @param type
     * @return
     */
    @Override
    public List<File> findByBusiness(String businessId, int type) {
        logger.info("根据业务id和类型查询文件接口 查询参数：业务Id,businessId:{}", "文件类型,type:{}", businessId, type);
        List<File> list = fileService.findByBusiness(businessId, type);
        logger.info("根据业务id和类型查询文件接口 返回数据：" + JSON.toJSONString(list));
        return list;
    }

    /**
     * 首页工单统计
     *
     * @return
     */
    @Override
    public JSONArray getOrder(RedisValue redisValue) {
        return facadeWorkOrderService.getOrderCount(redisValue);
    }

    /**
     * 未超期未完成的工单总数
     *
     * @param redisValue
     * @return
     */
    @Override
    public int getUnFinishOrder(RedisValue redisValue) {
        return facadeWorkOrderService.getUnFinishOrder(redisValue);
    }
}
